package com.plian.system.service.bpa.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.Tools.CollectionUtil;
import com.plian.system.constant.sys.FormStatusConstant;
import com.plian.system.entity.bpa.*;
import com.plian.system.mapper.bpa.BusinessAssessmentMapper;
import com.plian.system.model.R;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.bpa.*;
import com.plian.system.vo.bpa.*;
import com.plian.system.wrapper.bpa.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: jatpen
 * @Date: 2023/7/27 9:56 AM
 */
@Service
@AllArgsConstructor
@Slf4j
public class BusinessAssessmentServiceImpl extends BaseServiceImpl<BusinessAssessmentMapper, BusinessAssessment> implements IBusinessAssessmentService {

    private final BusinessAssessmentGearWrapper businessAssessmentGearWrapper;

    private final BusinessAssessmentScoreWrapper businessAssessmentScoreWrapper;

    private final BusinessAssessmentScoreDynamicWrapper businessAssessmentScoreDynamicWrapper;

    private final IBusinessAssessmentScoreService businessAssessmentScoreService;

    private final IBusinessAssessmentScoreDynamicService businessAssessmentScoreDynamicService;

    private final IBusinessAssessmentGearService businessAssessmentGearService;

    private final IBusinessAssessmentGearDynamicService businessAssessmentGearDynamicService;

    private final BusinessAssessmentGearDynamicWrapper businessAssessmentGearDynamicWrapper;

    private final BusinessAssessmentWrapper businessAssessmentWrapper;


    @Override
    @Transactional(rollbackFor = Throwable.class)
    public R submit(BusinessAssessmentVO businessAssessmentVO) {

        if (!Optional.ofNullable(businessAssessmentVO.getId()).isPresent()) {
            businessAssessmentVO.setStatus(FormStatusConstant.UNREVIEWED);
            businessAssessmentVO.setEnabled(0);
        }
        businessAssessmentVO.setModifyTime(new Date());
        BusinessAssessment businessAssessment = businessAssessmentWrapper.voToEntity(businessAssessmentVO);
        boolean result = super.saveOrUpdate(businessAssessment);

        /**
         * 档位配置
         */
        List<BusinessAssessmentGearVO> gearVOList = businessAssessmentVO.getGearList();
        if (CollectionUtil.isNotEmpty(gearVOList)) {
            for (BusinessAssessmentGearVO businessAssessmentGearVO : gearVOList) {
                if (businessAssessmentGearVO.getId() != null &&
                        Optional.ofNullable(businessAssessmentGearVO.getIsDeleted()).isPresent() && businessAssessmentGearVO.getIsDeleted().intValue() == 1) {
                    businessAssessmentGearService.removeById(businessAssessmentGearVO.getId());
                } else {
                    businessAssessmentGearVO.setBusinessAssessmentId(businessAssessment.getId());
                    BusinessAssessmentGear businessAssessmentGear = businessAssessmentGearWrapper.voToEntity(businessAssessmentGearVO);
                    businessAssessmentGearService.saveOrUpdate(businessAssessmentGear);
                    businessAssessmentGearVO.setId(businessAssessmentGear.getId());
                }

                List<BusinessAssessmentGearDynamicVO> gearDynamicVOList = businessAssessmentGearVO.getDynamicGearList();
                if (CollectionUtil.isNotEmpty(gearDynamicVOList)) {
                    for (BusinessAssessmentGearDynamicVO businessAssessmentGearDynamicVO : gearDynamicVOList) {
                        if (businessAssessmentGearDynamicVO.getId() != null &&
                                Optional.ofNullable(businessAssessmentGearDynamicVO.getIsDeleted()).isPresent() && businessAssessmentGearDynamicVO.getIsDeleted().intValue() == 1) {
                            businessAssessmentGearDynamicService.removeById(businessAssessmentGearDynamicVO.getId());
                        } else {
                            businessAssessmentGearDynamicVO.setGearId(businessAssessmentGearVO.getId());
                            businessAssessmentGearDynamicService.saveOrUpdate(businessAssessmentGearDynamicWrapper.voToEntity(businessAssessmentGearDynamicVO));
                        }
                    }

                }

            }

        }
        /**
         * 计分配置
         */
        List<BusinessAssessmentScoreVO> scoreVOList = businessAssessmentVO.getScoreList();
        if (CollectionUtil.isNotEmpty(scoreVOList)) {
            for (BusinessAssessmentScoreVO businessAssessmentScoreVO : scoreVOList) {
                if (businessAssessmentScoreVO.getId() != null &&
                        Optional.ofNullable(businessAssessmentScoreVO.getIsDeleted()).isPresent() && businessAssessmentScoreVO.getIsDeleted().intValue() == 1) {
                    businessAssessmentScoreService.removeById(businessAssessmentScoreVO.getId());
                } else {
                    businessAssessmentScoreVO.setBusinessAssessmentId(businessAssessment.getId());
                    BusinessAssessmentScore businessAssessmentScore = businessAssessmentScoreWrapper.voToEntity(businessAssessmentScoreVO);
                    businessAssessmentScoreService.saveOrUpdate(businessAssessmentScore);
                    businessAssessmentScoreVO.setId(businessAssessmentScore.getId());
                }
                List<BusinessAssessmentScoreDynamicVO> scoreDynamicVOS = businessAssessmentScoreVO.getDynamicScoreList();
                if (CollectionUtil.isNotEmpty(scoreDynamicVOS)) {
                    for (BusinessAssessmentScoreDynamicVO businessAssessmentScoreDynamicVO : scoreDynamicVOS) {
                        if (businessAssessmentScoreDynamicVO.getId() != null &&
                                Optional.ofNullable(businessAssessmentScoreDynamicVO.getIsDeleted()).isPresent() && businessAssessmentScoreDynamicVO.getIsDeleted().intValue() == 1) {
                            businessAssessmentScoreDynamicService.removeById(businessAssessmentScoreDynamicVO.getId());
                        } else {
                            businessAssessmentScoreDynamicVO.setScoreId(businessAssessmentScoreVO.getId());
                            businessAssessmentScoreDynamicService.saveOrUpdate(businessAssessmentScoreDynamicWrapper.voToEntity(businessAssessmentScoreDynamicVO));
                        }
                    }

                }

            }

        }
        return R.data(businessAssessment);
    }

    @Override
    public BusinessAssessmentVO detail(BusinessAssessment assessment) {


        if (Objects.nonNull(assessment)) {
            BusinessAssessmentVO businessAssessmentVO = businessAssessmentWrapper.entityToVO(assessment);

            List<BusinessAssessmentGearVO> businessAssessmentGearVOS = businessAssessmentGearWrapper.entityToVO(businessAssessmentGearService.list(Wrappers.<BusinessAssessmentGear>lambdaQuery()
                    .eq(BusinessAssessmentGear::getBusinessAssessmentId, businessAssessmentVO.getId())));

            if (CollectionUtil.isNotEmpty(businessAssessmentGearVOS)) {

                List<BusinessAssessmentGearDynamic> assessmentGearDynamics = businessAssessmentGearDynamicService.list(Wrappers.<BusinessAssessmentGearDynamic>lambdaQuery()
                        .in(BusinessAssessmentGearDynamic::getGearId, businessAssessmentGearVOS.stream().map(BusinessAssessmentGear::getId).collect(Collectors.toList())));

                for (BusinessAssessmentGearVO businessAssessmentGearVO : businessAssessmentGearVOS) {
                    List<BusinessAssessmentGearDynamic> gearDynamics = assessmentGearDynamics.stream().filter(e -> e.getGearId().equals(businessAssessmentGearVO.getId())).collect(Collectors.toList());
                    businessAssessmentGearVO.setDynamicGearList(businessAssessmentGearDynamicWrapper.entityToVO(gearDynamics));
                }
                businessAssessmentVO.setGearList(businessAssessmentGearVOS);
            }

            List<BusinessAssessmentScoreVO> businessAssessmentScoreVOS = businessAssessmentScoreWrapper.entityToVO(businessAssessmentScoreService.list(Wrappers.<BusinessAssessmentScore>lambdaQuery()
                    .eq(BusinessAssessmentScore::getBusinessAssessmentId, businessAssessmentVO.getId())));

            if (CollectionUtil.isNotEmpty(businessAssessmentScoreVOS)) {

                List<BusinessAssessmentScoreDynamic> assessmentScoreDynamics = businessAssessmentScoreDynamicService.list(Wrappers.<BusinessAssessmentScoreDynamic>lambdaQuery()
                        .in(BusinessAssessmentScoreDynamic::getScoreId, businessAssessmentScoreVOS.stream().map(BusinessAssessmentScore::getId).collect(Collectors.toList())));

                for (BusinessAssessmentScoreVO businessAssessmentScoreVO : businessAssessmentScoreVOS) {
                    List<BusinessAssessmentScoreDynamic> scoreDynamics = assessmentScoreDynamics.stream().filter(e -> e.getScoreId().equals(businessAssessmentScoreVO.getId())).collect(Collectors.toList());
                    businessAssessmentScoreVO.setDynamicScoreList(businessAssessmentScoreDynamicWrapper.entityToVO(scoreDynamics));
                }
                businessAssessmentVO.setScoreList(businessAssessmentScoreVOS);
            }
            return businessAssessmentVO;
        }
        return null;
    }
}
